package com.feicheng.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Adv;
import com.feicheng.persistence.entity.Image;
import com.feicheng.persistence.model.AdvPojo;
import com.feicheng.persistence.service.IAdvService;
import com.feicheng.persistence.service.IImageService;
import com.feicheng.service.IFAdvService;

@Service
public class FAdvServiceImpl implements IFAdvService{
	
	@Autowired
	IAdvService advService;
	
	@Autowired
	IImageService imageService;
	
	//广告详情查询接口
	@Override 
	public ResultInfo getAdvInfo(Integer advId) { 
	 //根据参数主键查询主表对应的信息 Adv
	 Adv advInfo = advService.getById(advId); 
	 //根据主表信息中的img_id查询附表所对应的信息 Image
	 Image imageInfo= imageService.getById(advInfo.getImgId()); 
	 //查询到的两条信息放到JSONObject中
	 JSONObject adv = new JSONObject(); 
	 adv.put("advInfo", advInfo);
	 adv.put("imageInfo",imageInfo);
	 
	 return ResultInfo.success(adv);
	
	}
	
	//用于分页查询广告接口
	@Override
	public ResultInfo getAdvByPage(Adv adv, Integer current, Integer size) {
		//新建pojo集合和条件构造器
		Page<AdvPojo> page = new Page<AdvPojo>(current, size);
		QueryWrapper<AdvPojo> queryWrapperAdvPojo = new QueryWrapper<>();
		//判断主表表中状态不为空
		if (adv.getStatu() != null) {
			queryWrapperAdvPojo.eq("a.statu",adv.getStatu());
		}
		
		return ResultInfo.success(advService.getAdvByPage(page, queryWrapperAdvPojo));
		
	}
	
	//用于修改广告页面接口
	@Transactional
	@Override
	public ResultInfo updateAdv(Adv adv, String url, Boolean imgStatu, Boolean Dflg) {
		//新建条件构造器获取主表主键
		QueryWrapper<Image> queryWapper = new QueryWrapper<Image>();
		queryWapper.eq("img_id", adv.getImgId());
		if (url != null) {
		Image image = imageService.getById(adv.getImgId());
		
		if (image.getDFlg()) {
			image.setUrl(url);
			image.setDFlg(false);
			imageService.save(image);
			adv.setImgId(image.getImgId());
			advService.updateById(adv);
			
		}else {
			image.setStatu(imgStatu);
			image.setDFlg(Dflg);
			image.setUrl(url);
			imageService.update(image,queryWapper);
			advService.updateById(adv);
		}
	}else {
		advService.updateById(adv);
	}
		return ResultInfo.success();
}
	
	//用于新增广告页面接口
	@Transactional
	@Override
	public ResultInfo insertAdv(Adv adv, String url, Boolean imgStatu, Boolean Dflg) {
		//新建附表并赋值储存
		Image image = new Image();
		image.setUrl(url);
		image.setStatu(imgStatu);
		image.setDFlg(Dflg);
		imageService.save(image);
		//获取附表主键
		adv.setImgId(image.getImgId());
		
		return ResultInfo.success(advService.save(adv));
	}
	
	//用于删除广告页面接口
	@Transactional
	@Override
	public ResultInfo deleteAdvInfo(Integer advId) {
		//通过主表服务类获取主表主键
		Adv adv = advService.getById(advId);
		//新建附表对象
		Image image = new Image();
		//通过主表中的主键关联附表
		image.setImgId(adv.getImgId());
		//将附表表的dFlg放入附表对象
		image.setDFlg(true);
		//根据附表主键修改信息
		imageService.updateById(image);
		//根据主表主键删除信息
		advService.removeById(advId);

		return ResultInfo.success();
	}

	@Override
	public ResultInfo getAdvWxInfo(Boolean statu) {
		
		
		QueryWrapper<AdvPojo> queryWrapperAdvPojo = new QueryWrapper<>();
		
		if (statu != null) {
			queryWrapperAdvPojo.eq("a.statu",statu);
		}
		return ResultInfo.success(advService.selectAdvPojoAll(queryWrapperAdvPojo));
	}
		
		
		
		
		

}
